package gdconf

import (
	"server_cluster/common/logger"
)

// 抽卡概率2配置表

const (
	Orange = 5
	Purple = 4
	Blue   = 3
)

type GachaPoolWeight struct {
	GachaPoolId int32
	Weight      int32
}

func (r *GachaPoolWeight) GetWeight() int32 {
	return r.Weight
}

type GachaProb2Config struct {
	Prob2Id         int32 `csv:"Poolgroup" gorm:"primary_key;autoIncrement:false"` // 概率规则2id
	RWeapon         int32 `csv:"R_Weapon,omitempty"`
	RWeaponWeight   int32 `csv:"R_Weaponweight,omitempty"`
	SRRole          int32 `csv:"Sr_Role,omitempty"`
	SRRoleWeight    int32 `csv:"Sr_Roleweight,omitempty"`
	SRWeapon        int32 `csv:"Sr_Weapon,omitempty"`
	SRWeaponWeight  int32 `csv:"Sr_Weaponweight,omitempty"`
	SSRRole         int32 `csv:"Ssr_Role,omitempty"`
	SSRRoleWeight   int32 `csv:"Ssr_Roleweight,omitempty"`
	SSRWeapon       int32 `csv:"Ssr_Weapon,omitempty"`
	SSRWeaponWeight int32 `csv:"Ssr_Weaponweight,omitempty"`

	GachaPoolWeightList []*GachaPoolWeight `gorm:"-" csv:"-"` // 蛋池权重列表
}

func (c *GachaProb2Config) TableName() string {
	return "config_gacha_prob2"
}

func (g *GameDataConfig) saveGachaProb2() {
	saveTableToDb[GachaProb2Config](g.Db, readTable[GachaProb2Config](g.CsvPathPrefix+"CardQuality.csv"))
}
func (g *GameDataConfig) loadGachaProb2Config() {
	g.GameDataMaps.GachaProb2Map = make(map[int32]*GachaProb2Config)

	for _, gachaProb2 := range loadTableFromDb[GachaProb2Config](g.Db) {
		if gachaProb2.RWeapon != 0 {
			checkGachaPoolItemType(gachaProb2.RWeapon, ITEM_TYPE_WEAPON)
			checkGachaPoolItemQuality(gachaProb2.RWeapon, Blue)
			gachaProb2.GachaPoolWeightList = append(gachaProb2.GachaPoolWeightList, &GachaPoolWeight{
				GachaPoolId: gachaProb2.RWeapon,
				Weight:      gachaProb2.RWeaponWeight,
			})
		}
		if gachaProb2.SRRole != 0 {
			checkGachaPoolItemType(gachaProb2.SRRole, ITEM_TYPE_ROLE)
			checkGachaPoolItemQuality(gachaProb2.SRRole, Purple)
			gachaProb2.GachaPoolWeightList = append(gachaProb2.GachaPoolWeightList, &GachaPoolWeight{
				GachaPoolId: gachaProb2.SRRole,
				Weight:      gachaProb2.SRRoleWeight,
			})
		}
		if gachaProb2.SRWeapon != 0 {
			checkGachaPoolItemType(gachaProb2.SRWeapon, ITEM_TYPE_WEAPON)
			checkGachaPoolItemQuality(gachaProb2.SRWeapon, Purple)
			gachaProb2.GachaPoolWeightList = append(gachaProb2.GachaPoolWeightList, &GachaPoolWeight{
				GachaPoolId: gachaProb2.SRWeapon,
				Weight:      gachaProb2.SRWeaponWeight,
			})
		}
		if gachaProb2.SSRRole != 0 {
			checkGachaPoolItemType(gachaProb2.SSRRole, ITEM_TYPE_ROLE)
			checkGachaPoolItemQuality(gachaProb2.SSRRole, Orange)
			gachaProb2.GachaPoolWeightList = append(gachaProb2.GachaPoolWeightList, &GachaPoolWeight{
				GachaPoolId: gachaProb2.SSRRole,
				Weight:      gachaProb2.SSRRoleWeight,
			})
		}
		if gachaProb2.SSRWeapon != 0 {
			checkGachaPoolItemType(gachaProb2.SSRWeapon, ITEM_TYPE_WEAPON)
			checkGachaPoolItemQuality(gachaProb2.SSRWeapon, Orange)
			gachaProb2.GachaPoolWeightList = append(gachaProb2.GachaPoolWeightList, &GachaPoolWeight{
				GachaPoolId: gachaProb2.SSRWeapon,
				Weight:      gachaProb2.SSRWeaponWeight,
			})
		}
		g.GameDataMaps.GachaProb2Map[gachaProb2.Prob2Id] = gachaProb2
	}

	logger.Info("GachaProb2Config Count: %v", len(g.GameDataMaps.GachaProb2Map))
}

func checkGachaPoolItemQuality(gachaPoolId int32, quality int32) {
	for _, gachaPool := range CONF.GameDataMaps.GachaPoolMap {
		if gachaPool.PoolId != gachaPoolId {
			continue
		}
		item := GetItemConfig(gachaPool.ItemId)
		if item == nil {
			continue
		}
		if item.Rarity != quality {
			logger.Error("卡池道具检查品质不匹配 道具id:%v 当前品质:%v 正确品质:%v", item.ItemId, item.Rarity, quality)
		}
	}
}

func checkGachaPoolItemType(gachaPoolId int32, _type int32) {
	for _, gachaPool := range CONF.GameDataMaps.GachaPoolMap {
		if gachaPool.PoolId != gachaPoolId {
			continue
		}
		item := GetItemConfig(gachaPool.ItemId)
		if item == nil {
			continue
		}
		if item.Type != _type {
			logger.Error("卡池道具检查类型不匹配 道具id:%v 当前类型:%v 正确类型:%v", item.ItemId, item.Type, _type)
		}
	}
}

func GetGachaProb2Map() map[int32]*GachaProb2Config { return CONF.GameDataMaps.GachaProb2Map }

func GetConfigGachaProb2ByProb2Id(prob2Id int32) *GachaProb2Config {
	data, ok := CONF.GameDataMaps.GachaProb2Map[prob2Id]
	if !ok {
		return nil
	}
	return data
}
